package Top200;

import java.util.Stack;

/**
 * @author zhangmin
 * @create 2022-02-18 19:50
 * 栈
 */
public class isValid20 {
    boolean ismatch(char left,char right){
        if ((left=='('&&right==')')||(left=='{'&&right=='}')||(left=='['&&right==']'))
            return true;
        return false;
    }
    public boolean isValid(String s) {
        Stack<Character> stack=new Stack<>();
        if (s.length()%2==1) return false;
        for (char c:s.toCharArray()) {
            if (stack.isEmpty()||c=='('||c=='{'||c=='['){
                stack.push(c);
            }else {
                if (ismatch(stack.peek(), c)){
                    stack.pop();
                }else {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
}
